/**
 * 类属性
 */
import tnx from '@/tnx.js';
import GenericType from './GenericType.js';

export default class ClassProperty {

    id = '';
    name = '';
    typeId = '';
    /**
     * @type {[GenericType]}
     */
    genericTypes = [];
    arrayed = false;
    caption = '';
    /**
     * 是否派生属性
     * @type {boolean}
     */
    derived = false;
    color = '';

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let property = new ClassProperty();
        property.update(object);
        property.ensureId();
        return property;
    }

    update(object) {
        tnx.util.object.rewriteSimpleDefined(this, object);
        if (typeof object === 'object') {
            if (object.genericTypes) {
                this.genericTypes = object.genericTypes.map(type => {
                    return GenericType.of(type);
                });
            }
        }
        return this;
    }

    clone() {
        return ClassProperty.of(this);
    }

    ensureId() {
        if (!this.id) {
            this.id = tnx.util.string.uuid32();
        }
    }

}
